#include "recovery/Status.h"
#include <sstream>

namespace recovery {

const char * Status::copy(const char * state, size_t size) const{
    if(size > 0){
        char * buf = new char[size + 1];    
        memcpy(buf, state, size);
        buf[size] = '\0';
        return buf;
    }
    return nullptr;
}
const char * Status::getCodeMsg(Code code) const{
    static const char * codeMsg [static_cast<int>(Code::kMaxCode)] = {
        " OK ",
        " Failed ",
        " Subcode ",
        " ErrorNo ", 
        " InvalidArgument ",
        " ArgumentNull ",
        " EndOfFile ",
        //TODO:
    };
    if(static_cast<int>(code) < static_cast<int>(Code::kMaxCode)){
        return codeMsg[static_cast<int>(code)];
    }
    return nullptr;
}
std::string Status::what() const {
    if(_code == Code::kOK){
        return " OK ";
    }

    std::ostringstream oss;
    if(auto * codeMsg = getCodeMsg(_code); codeMsg != nullptr){
        oss << codeMsg << ": " << ((_state == nullptr) ? "" : (const char *)_state);
    }
    else{
        oss << "unknown code(" << static_cast<int>(_code) << "): " << ((_state == nullptr) ? "" : (const char *)_state);
    }
    return oss.str();
}
Status Status::toStatus(const std::string_view & msg) const{
	return *this;
}

}// end of namespace recovery

